
  <template>
  <div class="bf-commont-template">
    <!-- <div style="padding: 0 16px ;">
                <h3>招聘需求</h3>
                <p>该页面代码已经自动生成，按照普通码 表增删改查 为基础生成模板</p>
                <p>生成代码对应的 vue 文件路径：D:\Zmyitem\vue-element-admin\src\beneform4j\view\hr\recruit\require\index.vue</p>
                <el-alert title="注意：表格内数据为模拟数据, 如果数据格式不对，请先进行格式转换。 模拟数据文件路径：src/mock/demo.js" type="warning"> </el-alert>
              </div> -->

    <div class="clearfix bf-top-button">
      <div class="float-l">
        <el-button @click="addreq" type="primary">新增需求</el-button>
      </div>
      <div class="float-r">
        <div class="float-l">
          <el-input placeholder="请输入需求名称搜索内容" prefix-icon="el-icon-search" v-model="searchData" @keyup.enter.native="quickSearch"></el-input>
        </div>
        <div class="float-l padding-l">
          <el-button @click="showSearch" type="primary">高级筛选</el-button>
        </div>
      </div>
    </div>
    <div class="margin-l margin-r">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane :label="'待提交'+ tabs.waitCount" status="0" name="waitSubmit">
          <bf-table :data="userData" :setting="setting" :rowAction="waitSubmitRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
        <el-tab-pane :label="'审批中' + tabs.approveCount" status="1" name="vetting">
          <bf-table :data="userData" :setting="setting" :rowAction="approveRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
        <el-tab-pane :label="'招聘中' + tabs.recruitCount" status="2" name="vetted">
          <bf-table :data="userData" :setting="setting" :rowAction="recruitRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
        <el-tab-pane :label="'停止招聘' + tabs.stopCount" status="8" name="stop">
          <bf-table :data="userData" :setting="setting" :rowAction="stopRowAction" @editSubmit="editSubmit" @pageChange="pageChange" ref="table"></bf-table>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 添加 -->
    <bf-table-edit :width="800" :rowNum="2" :editForm='detailSetting' :title="'添加需求'" :submitButtons="submitButtons" ref='addRosterRef' :editRules="addRules"></bf-table-edit>
    <!--高级搜索 -->
    <bf-table-search :editForm='searchSetting' :title="'高级搜索'" ref='searchRosterRef' @formSubmit='searchRosterSubmit'></bf-table-search>
    <!-- 编辑 -->
    <bf-table-edit :width="800" :rowNum="2" :submitButtons="editSubmitButtons" :editForm='detailSetting' :title="'需求编辑'" ref='convertFullRosterRef' :editRules="addRules"></bf-table-edit>
    <!-- 停止招聘原因-->
    <bf-table-edit :editForm='stopSetting' :title="'停止招聘详情'" ref='stopReasion' @formSubmit='stopRosterSubmit' :editRules="stopRules"></bf-table-edit>
    <!--详情-->
    <bf-table-detail :width="800" :rowNum="2" ref="bfTableDetail" :title="'需求详情'" :editForm="detailSetting" :showHistory='true' :approveSetting='approveSetting1'></bf-table-detail>

  </div>
</template>

<script>
import { findStatusCount, getRequireList, getReqdetail, addRequire, editRequire, stopRequire, deleteReq, giveupReq, updateBackSubmit } from './api.js'
import { searchSetting, setting, detailSetting, stopSetting } from './table.setting.js'
export default {
  data() {
    return {
      searchSetting: searchSetting,
      approveSetting1: {
        procDefKey: 'recruit-req',
        businessKey: ''
      },
      searchData: '',
      activeName: 'waitSubmit',
      statusData: {},
      submitButtons: [

        {
          title: '提交',
          click: (form) => {
            form.status = '1'
            // console.log(form)
            form.acton = '1'
            this.addRosterSubmit(form)
          }
        },
        {
          title: '暂存',
          click: (form) => {
            form.status = 0
            this.addRosterSubmit(form)
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.addRosterRef.hiden()
          }
        }
      ],


      editSubmitButtons: [
        {
          title: '暂存',
          click: (form) => {
            this.convertFullRosterSubmit(form)
          }
        },
        {
          title: '提交',
          click: (form) => {
            if (form.status === '0') {
              form.status = '1'
              form.action = '1'
              editRequire(form).then((response) => {
                if (response.success) {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                  this.$refs.addRosterRef.hiden()
                  this.$refs.convertFullRosterRef.hiden()
                  this.searchList('', this.getActivityStatus())
                  this.getTabs()
                }
              })
            } else {
              form.status = '1'
              form.action = '1'
              updateBackSubmit(form).then((response) => {
                if (response.success) {
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                  this.$refs.convertFullRosterRef.hiden()
                  this.searchList('', this.getActivityStatus())
                }
              })
            }
          }
        },
        {
          title: '取消',
          validate: false,
          click: (form) => {
            this.$refs.convertFullRosterRef.hiden()
          }
        }
      ],
      tabs: {
        waitCount: 0,
        approveCount: 0,
        recruitCount: 0,
        stopCount: 0,
      },
      userData: {
      },
      waitSubmitRowAction: [// 待提交
        {
          label: '查看',
          click: (item) => {
            this.approveSetting1.businessKey = item.scope.row.reqId
            this.findDetailById(item.scope.row.reqId, this.$refs.bfTableDetail.show)
          }
        },
        {
          label: '编辑',
          click: (item) => {
            if ((item.scope.row.taskDefKey === 'modify' || item.scope.row.taskDefKey === undefined) && (item.scope.row.status === '0' || item.scope.row.status === '1')) {
              this.findDetailById(item.scope.row.reqId, this.$refs.convertFullRosterRef.show)
            } else {
              this.$message({
                message: '该阶段不可编辑',
                type: 'error'
              })
            }
          }
        },
        {
          label: '提交',
          click: (item) => {
            if ((item.scope.row.taskDefKey === 'modify' || item.scope.row.taskDefKey === undefined) && (item.scope.row.status === '0' || item.scope.row.status === '1')) {
              this.reqSubmint(item.scope.row.reqId)
            } else {
              this.$message({
                message: '该阶段不可提交',
                type: 'error'
              })
            }
          }
        },
        {
          label: '删除',
          hiddenStatus: (row) => {
            if (row.status !== '0') {
              return false
            } else {
              return true
            }
          },
          click: (item) => {
            if (item.scope.row.status === '0') {
              this.$refs.table.showDelete(item)
            } else {
              this.$message({
                message: '退回修改状态不可删除',
                type: 'error'
              })
              return false
            }
          }
        },
        {
          label: '放弃申请',
          hiddenStatus: (row) => {
            if (row.status == '1' && row.taskDefKey == 'modify') {
              return true
            } else {
              return false
            }
          },
          click: (item) => {
            if (item.scope.row.taskDefKey === 'modify' && item.scope.row.status === '1') {
              this.giveup(item.scope.row.reqId)
            } else {
              this.$message({
                message: '未发起申请，可直接删除',
                type: 'error'
              })
              return false
            }


            // this.reqSubmint(item.scope.row.reqId)
          }
        }
      ],

      approveRowAction: [// 审批中
        {
          label: '查看',
          click: (item) => {
            this.approveSetting1.businessKey = item.scope.row.reqId
            this.findDetailById(item.scope.row.reqId, this.$refs.bfTableDetail.show)
            // this.$refs.bfTableDetail.show(item.scope)
          }
        }
      ],
      recruitRowAction: [// 招聘中
        {
          label: '查看',
          click: (item) => {
            this.approveSetting1.businessKey = item.scope.row.reqId
            this.findDetailById(item.scope.row.reqId, this.$refs.bfTableDetail.show)
            // this.$refs.bfTableDetail.show(item.scope)
          }
        },
        {
          label: '停止招聘',
          click: (item) => {
            this.$refs.stopReasion.show(item.scope)
          }
        }
      ],
      stopRowAction: [// 停止招聘
        {
          label: '查看',
          click: (item) => {
            this.approveSetting1.businessKey = item.scope.row.reqId
            this.findDetailById(item.scope.row.reqId, this.$refs.bfTableDetail.show)
          }
        }
      ],
      setting: setting,
      detailSetting: detailSetting,
      stopSetting: stopSetting,
      addRules: {
        orgId: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        reqName: [
          { required: true, message: '请输入需求名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 3 到 50个字符', trigger: 'blur' }
        ],
        planId: [
          { required: true, message: '请选择招聘规划', trigger: 'blur' },
        ],
        position: [
          { required: true, message: '请选择岗位名称', trigger: 'blur' },
        ],
        recruitsNumbers: [
          { required: true, message: '请输入招聘人数', trigger: 'blur' }
        ],
        workCity: [
          { min: 2, max: 50, message: '长度在 2 到 50个字符', trigger: 'blur' }
        ],
        startingSalary: [
          { required: true, message: '请输入起始薪资', trigger: 'blur' }
        ],
        topSalary: [
          { required: true, message: '请输入最高薪资', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '请输入启动日期', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '请输入最晚入职日期', trigger: 'blur' }
        ],
        responsiblePerson: [
          { required: true, message: '请选择负责人', trigger: 'blur' }
        ]
      },
      stopRules: {
        stopReason: [
          { required: true, message: '请输入停止招聘原因', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50个字符', trigger: 'blur' }
        ]
      },
      searchParams: { // 搜索条件
        page: 1
      }
    }
  },
  mounted: function() {
    this.searchList('', 0)
    this.getTabs()
  },
  methods: {
    addRosterSubmit(form) { // 新增
      addRequire(form).then((response) => {
        if (response.success) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.$refs.addRosterRef.hiden()
          this.searchList('', this.getActivityStatus())
          this.getTabs()
        }
      })
    },
    searchRosterSubmit(form) { // 高级搜索
      this.searchParams.orgId = form.orgId
      this.searchParams.reqId = form.reqId
      this.searchParams.reqName = form.reqName

      this.searchList()
      this.$refs.searchRosterRef.hiden()
    },
    convertFullRosterSubmit(form) { // 保存
      if (form.status === '1') {
        form.status = 0
        form.action = 1
        updateBackSubmit(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.$refs.convertFullRosterRef.hiden()
            this.searchList('', this.getActivityStatus())
          }
        })
      } else {
        form.status = 0
        form.action = 1
        editRequire(form).then((response) => {
          if (response.success) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.$refs.convertFullRosterRef.hiden()
            this.searchList('', this.getActivityStatus())
          }
        })
      }
    },
    reqSubmint(reqId) { // 提交
      getReqdetail(reqId).then((response) => {
        if (response.data.status === '0') {
          response.data.status = '1'
          response.data.action = '1'
          editRequire(response.data).then((response) => {
            if (response.success) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.$refs.addRosterRef.hiden()
              this.$refs.convertFullRosterRef.hiden()
              this.searchList('', this.getActivityStatus())
              this.getTabs()
            }
          })
        } else {
          response.data.status = '1'
          response.data.action = '1'
          updateBackSubmit(response.data).then((response) => {
            if (response.success) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.$refs.convertFullRosterRef.hiden()
              this.searchList('', this.getActivityStatus())
            }
          })
        }
      })
    },



    giveup(reqId) {
      getReqdetail(reqId).then((response) => {
        giveupReq(response.data).then((response) => { // 放弃申请
          if (response.success) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.searchList('', this.getActivityStatus())
            this.getTabs()
          }
        })
      })
    },
    stopRosterSubmit(form) {
      stopRequire(form).then((response) => { // 停止招聘
        if (response.success) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.$refs.stopReasion.hiden()
          this.searchList('', this.getActivityStatus())
          this.getTabs()
        }
      })
    },
    getTabs(forms) {
      findStatusCount(forms).then((request) => {
        this.statusData = request.data
        this.tabs.waitCount = request.data[0].count4Status0
        this.tabs.approveCount = request.data[0].count4Status1
        this.tabs.recruitCount = request.data[0].count4Status2
        this.tabs.stopCount = request.data[0].count4Status8
      })
    },
    findDetailById(id, showCall) { // 详情
      getReqdetail(id).then((response) => {
        showCall(response.data)
      })
    },
    findReqdetail(id) { // 详情
      getReqdetail(id).then((response) => {
        this.userData = response;
      })
    },
    searchList() {
      const pageParams = this.searchParams
      pageParams.status = this.getActivityStatus()
      getRequireList(pageParams).then((response) => {
        if (response.list) {
          this.userData = response
        }
      })
    },

    editSubmit(params, formType, formList) {
      // console.log(params, formType, formList)
      if (formType === 'add') {
        // this.addItem(params)
      } else if (formType === 'search') {
        // this.searchList(params)
      } else if (formType === 'delete') {
        const reqId = params.row.reqId
        this.deleteItem(reqId)
      } else if (formType === 'edit') {
        // this.updataItem(params, params.userId)
      }
    },

    deleteItem(reqId) {
      deleteReq(reqId).then((response) => {
        if (response.success) {
          this.$message({
            message: '删除成功!',
            type: 'success'
          })
          this.searchList('', this.getActivityStatus())
          this.getTabs()
        }
      })
    },
    pageChange(pageObj) {
      this.searchParams.page = pageObj.page
      this.searchParams.pageSize = pageObj.pageSize
      this.searchList()
    },
    showSearch() {
      this.$refs.searchRosterRef.show()
    },
    addreq() {
      const params = {
        orgId: '',
        position: '',
        startingSalary: '',
        topSalary: ''
      }
      this.$refs.addRosterRef.show(params)
    },
    quickSearch() { // 快捷搜索
      this.searchParams.page = 1
      this.searchParams.reqName = this.searchData
      this.searchList()
    },
    handleClick(tab, event) {
      this.searchParams.page = 1
      this.searchList()
    },
    getActivityStatus() {
      if (this.activeName === 'waitSubmit') {
        return 0;
      }
      if (this.activeName === 'vetting') {
        return 1;
      }
      if (this.activeName === 'vetted') {
        return 2;
      }
      if (this.activeName === 'stop') {
        return 8;
      }
    }
  }
}
</script>

<style scoped>

</style>
